package com.rui1024.dao.impl;

import com.rui1024.dao.UserDao;
import com.rui1024.pojo.User;
import com.rui1024.util.Md5Util;
import com.rui1024.util.PropertiesUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;


public class UserDaoImpl implements UserDao {

    @Override
    public User login(Connection con, User user) throws Exception{
        String sql="select * from t_user where username=? and password=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getUsername());
        pstmt.setString(2,Md5Util.encoderPwdByMd5(user.getPassword()));
        System.out.println("加密后密码为"+Md5Util.encoderPwdByMd5(user.getPassword()));
        ResultSet rs = pstmt.executeQuery();
        User currentUser=null;
        if (rs.next()){
            currentUser= new User();
            currentUser.setUserId(rs.getInt("userId"));
            currentUser.setUsername(rs.getString("username"));
            currentUser.setPassword(rs.getString("password"));
            currentUser.setNickName(rs.getString("nickName"));
            currentUser.setImageName(PropertiesUtil.getValue("imageFile")+rs.getString("imageName"));
            currentUser.setMood(rs.getString("mood"));
        }
        return currentUser;
    }

    @Override
    public int userUpdate(Connection con, User user) throws Exception {
        String sql="update t_user set nickName=?,imageName=?,mood=? where userId=?";
        PreparedStatement pstmt = con.prepareStatement(sql);
        pstmt.setString(1,user.getNickName());
        pstmt.setString(2,user.getImageName());
        pstmt.setString(3,user.getMood());
        pstmt.setInt(4,user.getUserId());
        return pstmt.executeUpdate();
    }
}
